package com.mls.guardian.executor.master;

import com.mls.guardian.container.MasterContainer;
import com.mls.guardian.protocol.RemoteBody;
import com.mls.guardian.protocol.RemoteCommand;
import com.mls.guardian.protocol.body.ClientInactiveBody;
import com.mls.guardian.remoting.client.impl.NettyClientConnect;
import com.mls.guardian.remoting.executor.RemoteExecutor;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by jl on 15/10/23.
 */
public class InactiveExecutor implements RemoteExecutor {
    private static final Logger log = LoggerFactory.getLogger(InactiveExecutor.class);
    private MasterContainer container;

    public InactiveExecutor(MasterContainer container){
        this.container = container;
    }
    @Override
    public RemoteBody execute(ChannelHandlerContext ctx, RemoteCommand request) throws Exception {
        ClientInactiveBody clientInactive = (ClientInactiveBody) request.getRemoteBody();

        NettyClientConnect clientConnect = new  NettyClientConnect(ctx.channel());
        Object obj = container.getClientDispatch().removeClient(clientInactive.getAddr(),clientConnect);

        if(obj != null){
            log.info("id[{}] code[{}] slave client {} disconnect",request.getRequestId(),request.getCode(),ctx.channel().remoteAddress());
        }else{
            log.info("id[{}] code[{}] non slave client {} disconnect",request.getRequestId(),request.getCode(),ctx.channel().remoteAddress());
        }
        return null;
    }
}
